#include "stdafx.h"
#include "dataProcessDef.h"
#include "dataProcess.h"
#include "datastruct.h"
#include "SeverData.h"
#include "dataManage.h"
#include "dataManageDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

bool GetAreaInfo(netData* netDataIn,netData* netDataOut,bool isSend)
{
	if (!isSend)
	{
		CSeverData* theData = theApp.m_serverData;
		int bufferSize = 0;

		if (netDataOut->buffer)
		{
			delete []netDataOut->buffer;
		}
		netDataOut->type = dataProcessTypeSetAeraInfo;

		theData->Lock();
		int nCount = theData->getAreaCount();
		netDataOut->bufferLength = sizeof(nCount);
		for (int i = 0; i < nCount; i++)
		{
			netDataOut->bufferLength += theData->getAreaInfo(i).save(NULL);
		}

		netDataOut->buffer = new char[netDataOut->bufferLength];
		void* tempBuffer = netDataOut->buffer;

		MAC_WRITEVAR(tempBuffer,int,nCount,bufferSize);
		for (int i = 0; i < nCount; i++)
		{
			bufferSize = theData->getAreaInfo(i).save(tempBuffer);
			tempBuffer = (char*)tempBuffer + bufferSize;
		}
		theData->UnLock();
		return true;
	}
	else
	{
		return false;
	}
}

bool CheckUserName(netData* netDataIn,netData* netDataOut,bool isSend)
{
	userAccount* userInfo = (userAccount*)netDataIn->buffer;
	userAccount rightInfo;
	if (!isSend)
	{
		memset(&rightInfo,0,sizeof(userAccount));
		CSeverData* theData = theApp.m_serverData;
		theData->Lock();
		int right = 0;
		if(theData->checkPasswdIsRight(*userInfo,right))
		{
			memcpy(rightInfo.username,userInfo->username,50 * sizeof(wchar_t));
			rightInfo.right = right;
		}
		theData->UnLock();
		netDataOut->type = setUserNameRight;
		netDataOut->bufferLength = sizeof(userAccount);
		netDataOut->buffer = new char[netDataOut->bufferLength];
		memcpy(netDataOut->buffer,&rightInfo,netDataOut->bufferLength);
		return true;
	}
	return false;
}

bool getComName(netData* netDataIn,netData* netDataOut,bool isSend)
{
	if (!isSend)
	{
		CdataManageDlg* theMainDlg = (CdataManageDlg*)theApp.GetMainWnd();
		const CString& temp = theMainDlg->getComName();
		netDataOut->bufferLength = (temp.GetLength() + 1) * sizeof(wchar_t);
		netDataOut->buffer = new char[netDataOut->bufferLength];
		memset(netDataOut->buffer,0,netDataOut->bufferLength);
		memcpy(netDataOut->buffer,temp,temp.GetLength()*2);
		netDataOut->type = setCompanyName;
		return true;
	}
	return false;
}

bool getPicture(netData* netDataIn,netData* netDataOut,bool isSend)
{
	if (!isSend)
	{
		CdataManageDlg* theMainDlg = (CdataManageDlg*)theApp.GetMainWnd();
		const CString& temp = theMainDlg->getComPicturePath();
		CFile picFile(CdataManageApp::m_AppPath + temp,CFile::modeRead);
		netDataOut->bufferLength = (int)picFile.GetLength();
		netDataOut->buffer = new char[netDataOut->bufferLength];
		memset(netDataOut->buffer,0,netDataOut->bufferLength);
		unsigned int  ncount = picFile.Read(netDataOut->buffer,netDataOut->bufferLength);
		picFile.Close();
		netDataOut->type = setCompanyPicture;
		return true;
	}
	return false;
}

bool getEmployeeSI(netData* netDataIn,netData* netDataOut,bool isSend)
{
	if (!isSend)
	{
		CSeverData* theData = theApp.m_serverData;
		employeeShortInfo shortInfo;
		CString shortId((wchar_t*)netDataIn->buffer);
		theData->getEmployeeShort(shortInfo,shortId);
		netDataOut->type = setEmployeeShortInfo;
		netDataOut->bufferLength = sizeof(employeeShortInfo);
		netDataOut->buffer = new char[netDataOut->bufferLength];
		memcpy(netDataOut->buffer,&shortInfo,netDataOut->bufferLength);
		return true;
	}
	return false;
}

bool getBookSetData(netData* netDataIn,netData* netDataOut,bool isSend)
{
	if (!isSend)
	{
		CdataManageDlg* theMainDlg = (CdataManageDlg*)theApp.GetMainWnd();
		bookSetData bookData;
		theMainDlg->getBookData(bookData);
		netDataOut->type = dataProcessSetBookSetData;
		netDataOut->bufferLength = bookData.save(NULL);
		netDataOut->buffer = new char[netDataOut->bufferLength];
		bookData.save(netDataOut->buffer);
		return true;
	}
	return false;
}

bool getBookData(netData* netDataIn,netData* netDataOut,bool isSend)
{
	if (!isSend)
	{
		CSeverData* theData = theApp.m_serverData;
		vector<bookManagerData> vData;
		getBookManagerData* pData = (getBookManagerData*)netDataIn->buffer;
		CTime nowTime(pData->m_date.year,pData->m_date.month,pData->m_date.day,0,0,0);
		theData->GetBookData(vData,nowTime,pData->m_deskName,pData->m_areaName);
		netDataOut->type = dataProcessSetBookData;
		netDataOut->bufferLength = sizeof(int) + (int)vData.size() * sizeof(bookManagerData);
		netDataOut->buffer = new char[netDataOut->bufferLength];
		void* pBuffer = netDataOut->buffer;

		int nCount = (int)vData.size();
		int nSize = 0;
		MAC_WRITEVAR(pBuffer,int,nCount,nSize);
		for (int i = 0; i < nCount; i++)
		{
			MAC_WRITEVAR(pBuffer,bookManagerData,vData[i],nSize);
		}
		return true;
	}
	return false;
}

bool addBookData(netData* netDataIn,netData* netDataOut,bool isSend)
{
	if (!isSend)
	{
		CSeverData* theData = theApp.m_serverData;
		newBookData* pNewData = (newBookData*)netDataIn->buffer;
		theData->addBookData(*pNewData);
		return false;
	}
	return false;
}

bool delBookData(netData* netDataIn,netData* netDataOut,bool isSend)
{
	if (!isSend)
	{
		CSeverData* theData = theApp.m_serverData;
		hjcDateTime* pTime = (hjcDateTime*)(netDataIn->buffer);
		theData->delBookData(pTime->m_time,pTime->m_date);
		return false;
	}
	return false;
}

bool getOrder(netData* netDataIn,netData* netDataOut,bool isSend)
{
	if (!isSend)
	{
		CSeverData* theData = theApp.m_serverData;
		getDeskOrder* pSerial = (getDeskOrder*)netDataIn->buffer;
		INT64 nSerial = theData->getSerial(pSerial->areaName,pSerial->deskName);
		Order order;
		theData->getOrder(nSerial,order);
		netDataOut->bufferLength = order.save(NULL);
		netDataOut->buffer = new char[netDataOut->bufferLength];
		order.save(netDataOut->buffer);
		netDataOut->type = dataProcessSetOrder;
		return true;
	}
	return false;
}

bool getFoodParam(netData* netDataIn,netData* netDataOut,bool isSend)
{
	if (!isSend)
	{
		CSeverData* theData = theApp.m_serverData;
		wchar_t* pId = (wchar_t*)netDataIn->buffer;
		foodParam food = theData->getFoodParamByID(pId);
		if (food.name[0] == 0)
		{
			food = theData->getFoodParamByAbbr(pId);
		}

		netDataOut->bufferLength = sizeof(food);
		netDataOut->buffer = new char[netDataOut->bufferLength];
		memcpy(netDataOut->buffer,&food,netDataOut->bufferLength);

		netDataOut->type = dataProcessSetFoodParam;
		return true;
	}
	return false;
}
bool getOrderOption(netData* netDataIn,netData* netDataOut,bool isSend)
{
	if (!isSend)
	{
		orderOption* pOption = new orderOption;
		memset(pOption,0,sizeof(orderOption));
		CdataManageDlg* theMainDlg = (CdataManageDlg*)theApp.GetMainWnd();
		pOption->defaultNum = theMainDlg->m_OrderNumber;
		pOption->OrderPrepare = theMainDlg->m_OrderPrepare;
		CString temp;
		for (size_t i = 0; i < theMainDlg->m_OrderRequire.size(); i++)
		{
			temp.Append(theMainDlg->m_OrderRequire[i]);
			temp.Append(L";");
		}
		memcpy(pOption->require,temp,max(50,temp.GetLength() - 1) * sizeof(wchar_t));
		netDataOut->bufferLength = sizeof(orderOption);
		netDataOut->buffer = new char[netDataOut->bufferLength];
		memcpy(netDataOut->buffer,pOption,sizeof(orderOption));
		netDataOut->type = dataProcessSetOrderOption;
		delete pOption;
		return true;
	}
	return false;
}

bool addOrder(netData* netDataIn,netData* netDataOut,bool isSend)
{
	if (!isSend)
	{
		Order newOrder;
		newOrder.load(netDataIn->buffer);

		CSeverData* theData = theApp.m_serverData;
		theData->updateOrder(newOrder.nSerial,newOrder);
		return false;
	}
	return false;
}

bool getSerial(netData* netDataIn,netData* netDataOut,bool isSend)
{
	//if (!isSend)
	//{
	//	getOrderSerial* pSerial = (getOrderSerial*)netDataIn->buffer;
	//	CSeverData* theData = theApp.m_serverData;
	//	pSerial->nSerial = theData->getSerial(pSerial->areaName,pSerial->deskName);
	//	netDataOut->bufferLength = sizeof(getOrderSerial);
	//	netDataOut->buffer = new char[netDataOut->bufferLength];
	//	memcpy(netDataOut->buffer, pSerial,sizeof(getOrderSerial));
	//	netDataOut->type = dataProcessSetSerial;
	//	return false;
	//}
	return false;
}

bool getAllDiscountPlan(netData* netDataIn,netData* netDataOut,bool isSend)
{
	if (!isSend)
	{
		CdataManageDlg* theMainDlg = (CdataManageDlg*)theApp.GetMainWnd();
		int nSize = 0;
		int nCount = (int)theMainDlg->getDiscountNameVec().size();
		nSize += 4;
		vector<discountPlan> vDis;
		for (int i = 0; i < nCount; i++)
		{
			discountPlan plan;
			wcscpy_s(plan.discountName,theMainDlg->getDiscountNameVec()[i]);
			theApp.m_serverData->getDiscount(plan.vDiscount,plan.discountName);
			nSize += plan.save(NULL);
			vDis.push_back(plan);
		}
		netDataOut->bufferLength = nSize;
		netDataOut->buffer = new char[netDataOut->bufferLength];
		netDataOut->type = dataProcessSetAllDiscountPlan;
		void* tmpBuffer = netDataOut->buffer;
		nSize = 0;
		MAC_WRITEVAR(tmpBuffer,int,nCount,nSize);
		for (int i = 0; i < nCount; i++)
		{
			nSize = vDis[i].save(tmpBuffer);
			tmpBuffer = (char*)tmpBuffer + nSize;
		}
		return true;
	}
	return false;
}
bool checkOutDesk(netData* netDataIn,netData* netDataOut,bool isSend)
{
	if (!isSend)
	{
		getDeskOrder* pDesk =(getDeskOrder* ) netDataIn->buffer;
		CSeverData* theData = theApp.m_serverData;
		theData->checkOut(pDesk->areaName,pDesk->deskName);
	}
	return false;
}
bool setState(netData* netDataIn,netData* netDataOut,bool isSend)
{
	if (!isSend)
	{
		setDeskState* pDesk =(setDeskState* ) netDataIn->buffer;
		CSeverData* theData = theApp.m_serverData;
		theData->setState(pDesk);
	}
	return false;

}
bool processMain(void* paramIn,void* paramOut,bool isSend/* = true*/)
{
	netData* netDataIn = (netData*)paramIn;
	netData* netDataOut = (netData*)paramOut;
	switch (netDataIn->type)
	{
	case dataProcessTypeGetAeraInfo:
		return GetAreaInfo(netDataIn,netDataOut,isSend);
	case checkUserNameIsRight:
		return CheckUserName(netDataIn,netDataOut,isSend);
	case getCompanyName:
		return getComName(netDataIn,netDataOut,isSend);
	case getCompanyPicture:
		return getPicture(netDataIn,netDataOut,isSend);
	case getEmployeeShortInfo:
		return getEmployeeSI(netDataIn,netDataOut,isSend);
	case dataProcessGetBookSetData:
		return getBookSetData(netDataIn,netDataOut,isSend);
	case dataProcessGetBookData:
		return getBookData(netDataIn,netDataOut,isSend);
	case dataProcessAddBookData:
		return addBookData(netDataIn,netDataOut,isSend);
	case dataProcessDelBookData:
		return delBookData(netDataIn,netDataOut,isSend);
	case dataProcessGetOrder:
		return getOrder(netDataIn,netDataOut,isSend);
	case dataProcessGetFoodParam:
		return getFoodParam(netDataIn,netDataOut,isSend);
	case dataProcessGetOrderOption:
		return getOrderOption(netDataIn,netDataOut,isSend);
	case dataProcessUpdateOrder:
		return addOrder(netDataIn,netDataOut,isSend);
	case dataProcessGetSerial:
		return getSerial(netDataIn,netDataOut,isSend);
	case dataProcessGetAllDiscountPlan:
		return getAllDiscountPlan(netDataIn,netDataOut,isSend);
	case dataProcessCheckOut:
		return checkOutDesk(netDataIn,netDataOut,isSend);
	case dataProcessSetState:
		return setState(netDataIn,netDataOut,isSend);
	}
	return false;
}